export const validatePhoneNumber = () => {
  return [
    {
      required: true,
      message: "请输入11位手机号",
      trigger: "change",
    },
    {
      required: true,
      trigger: "blur",
      min: 11,
      max: 11,
      message: "长度不符合",
    },
    {
      required: true,
      trigger: "blur",
      validator: (rule, value, callback) => {
        // 判断输入框中是否输入手机号
        if (!value) {
          callback(new Error("手机号不能为空"));
        }
        //正则表达式进行验证手机号，从1开始，第二位是35789中的任意一位，以9数字结尾
        if (!/^1[35789]\d{9}$/.test(value)) {
          callback(new Error("手机号格式不正确"));
        }
        callback();
      },
    },
  ];
};
export const validateNumber = () => {
  return [
    {
      required: true,
      message: "该值不能为空",
      trigger: "change",
    },
    {
      required: true,
      validator: (rule, value, callback) => {
        if (value !== "" && value !== null && value !== undefined) {
          if (/^\d+$/.test(value) == false) {
            callback(new Error("只允许输入数字"));
          } else {
            callback();
          }
        } else {
          callback("不能为空");
        }
      },
      trigger: "blur",
    },
  ];
};
export const validateCard = () => {
  return [
    {
      required: true,
      message: "请输入身份证号",
      trigger: "change",
    },
    {
      required: true,
      validator: (rule, value, callback) => {
        // 1 "验证通过!", 0 //校验不通过 // id为身份证号码
        let format =
          /^(([1][1-5])|([2][1-3])|([3][1-7])|([4][1-6])|([5][0-4])|([6][1-5])|([7][1])|([8][1-2]))\d{4}(([1][9]\d{2})|([2]\d{3}))(([0][1-9])|([1][0-2]))(([0][1-9])|([1-2][0-9])|([3][0-1]))\d{3}[0-9xX]$/;
        //号码规则校验
        if (!format.test(value)) {
          callback("身份证号码不合规");
        }
        //区位码校验
        //出生年月日校验  前正则限制起始年份为1900;
        let year = value.substr(6, 4), //身份证年
          month = value.substr(10, 2), //身份证月
          date = value.substr(12, 2), //身份证日
          time = Date.parse(month + "-" + date + "-" + year), //身份证日期时间戳date
          now_time = Date.parse(new Date()), //当前时间戳
          dates = new Date(year, month, 0).getDate(); //身份证当月天数
        if (time > now_time || date > dates) {
          return { status: 0, msg: "出生日期不合规" };
        }
        //校验码判断
        let c = new Array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2); //系数
        let b = new Array(
          "1",
          "0",
          "X",
          "9",
          "8",
          "7",
          "6",
          "5",
          "4",
          "3",
          "2"
        ); //校验码对照表
        let value_array = value.split("");
        let sum = 0;
        for (let k = 0; k < 17; k++) {
          sum += parseInt(value_array[k]) * parseInt(c[k]);
        }
        if (value_array[17].toUpperCase() != b[sum % 11].toUpperCase()) {
          callback("身份证校验不合规！");
        }
        callback();
      },
      trigger: "blur",
    },
  ];
};
